$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/borderinfo/list',
        datatype: "json",
        colModel: [
			{ label: '订单号', name: 'orderId', index: 'order_id',align: "center", width: 90, key: true,
				formatter:function(value,options,rowData){
					return "<a onclick=vm.openInfo('"+value+"')>"+value+"</a>";
				}
			},
			{ label: '用户ID', name: 'userId', index: 'user_id',align: "center", width: 50 ,
				formatter:function(value,options,rowData){
					return "<a  onclick='window.open(\"../sys/buserinfo.html?userId="+value+"\")'>"+value+"</a>";
				}
			},
			{ label: '用户昵称', name: 'userName', index: 'userName',align: "center", width: 80 },
			{ label: '点击支付(次)', name: 'orderCount', index: 'orderCount',align: "center", width: 50,
				formatter: function (value, options, rowData) {
					let count = rowData["count"];
					if (count > 2) {
						return "<p style='color: red'>" + value + "</>";
					} else {
						return "<p'>" + value + "</>";
					}
				}
			},
			{ label: '访问会员页(次)', name: 'pageCount', index: 'pageCount',align: "center" , width: 60},
			{ label: '品牌', name: 'brandName', index: 'brandName',align: "center", width: 70 },
            { label: '公众号', name: 'gzhName', index: 'gzhName',align: "center", width:60 },
			{ label: '交易金额', name: 'totalAmount', index: 'total_amount',align: "center", width: 45 },
			{ label: '交易状态', name: 'transStatus', index: 'trans_status',align: "center", width: 45,
				formatter:function(value,options,rowData){
					if(value==0){
						return "待支付";
					}else if(value==1){
						return "<p style='color: green'>已支付</p>";
					}else if(value==2){
                        return "<p style='color: red'>已退款</p>";
                    }
				}
			},
			{ label: '第三方交易号', name: 'tradeNo', index: 'trade_no',align: "center", width: 60 },
			// { label: '交易说明', name: 'itemId', index: 'item_id',align: "center", width: 80,
			// 	formatter:function(value,options,rowData){
			// 		if(value==1){
			// 			return "半年会员";
			// 		}else if(value==2){
			// 			return "一年会员";
			// 		}else if(value==3){
			// 			return "三年会员";
			// 		}else{
			// 			return "后台虚拟会员"
			// 		}
			// 	}
			// },
            { label: '交易说明', name: 'itemName', index: 'itemName',align: "center", width: 45},
			{ label: '交易时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			{ label: '跟进时间', name: 'followTime', index: 'follow_time',align: "center", width: 80 },
			{ label: '跟进人', name: 'followUserName', index: 'followUserName',align: "center", width: 50 },
        ],
		viewrecords: true,
        height: "100%",
        rowNum: 18,
		rowList : [18,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });


	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bOrderInfo: {},
		q:{
			userId:null,
			userName:null,
			gzhCode:'',
			transStatus:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bOrderInfo = {};
		},
		update: function (event) {
			var orderId = getSelectedRow();
			if(orderId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(orderId)
		},
		openInfo: function (e) {
			var orderId =e;
			if(orderId == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(orderId)
		},
		saveOrUpdate: function (event) {
			var url = vm.bOrderInfo.orderId == null ? "sys/borderinfo/save" : "sys/borderinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bOrderInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        drawback: function (event) {

            confirm('确定要退款吗？退款后，该用户的vip状态会撤销。', function(){

                var url = "sys/borderinfo/drawback";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bOrderInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            });
        },
		del: function (event) {
			var orderIds = getSelectedRows();
			if(orderIds == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/borderinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(orderIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(orderId){
			$.get(baseURL + "sys/borderinfo/info/"+orderId, function(r){
                vm.bOrderInfo = r.bOrderInfo;
				// if(vm.bOrderInfo.itemId==1){
				// 	vm.bOrderInfo.itemId="半年会员";
				// }else if(vm.bOrderInfo.itemId==2){
				// 	vm.bOrderInfo.itemId= "一年会员";
				// }else if(vm.bOrderInfo.itemId==3){
				// 	vm.bOrderInfo.itemId= "三年会员";
				// }
                $("#drawback").hide();
				if(vm.bOrderInfo.transStatus==0){
					vm.bOrderInfo.transStatusText="待支付";
				}else if(vm.bOrderInfo.transStatus==1){
                    vm.bOrderInfo.transStatusText="已支付";
                    $("#drawback").show();
                }else if(vm.bOrderInfo.transStatus==2){
                    vm.bOrderInfo.transStatusText="已退款";
                }

            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page,
				postData:{"userId":vm.q.userId,"userName":vm.q.userName,"gzhCode":vm.q.gzhCode,"transStatus":vm.q.transStatus}
            }).trigger("reloadGrid");
		}
	}
});
